package ldk.util.layoutmanagers;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AutoSpanGridLayoutManager;
import android.support.v7.widget.CenterGridLayoutManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ldk.util.layoutmanagers.adapter.BaseAdapter;
import ldk.util.layoutmanagers.adapter.TagsAdapter;
import ldk.util.layoutmanagers.adapter.TagsViewHolder;

/**
 * 居中的LayoutManager
 * Created by ldkxingzhe@163.com on 2017/8/21.
 */
public class AutoSpanGridLayoutManagerActivity extends Activity{
    private static final String TAG = "CenterLayoutManager";

    RecyclerView recyclerView;
    AutoSpanGridLayoutManager gridLayoutManager;
    private List<String> dataList = new ArrayList<>();
    private TagsAdapter adapter = new TagsAdapter(dataList);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_center_layout);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        gridLayoutManager = new AutoSpanGridLayoutManager(this, 4);
        recyclerView.setLayoutManager(gridLayoutManager);
        gridLayoutManager.setSpanSizeLookup(spanSizeLookup);
        dataList.addAll(Arrays.asList("中国1", "美国", "俄罗斯", "德国", "法国1", "意大利", "日本", "韩国", "英国1", "西班牙",
                "吃的", "喝的百事可乐还是可口可乐", "谁的天涯明月刀", "我的中华人民共和国", "天涯", "令牌1", "汉堡, 鸡翅， 牛排， 盒饭",
                "炸鸡", "肥牛", "又一只肥牛， 可以了吗", "鸡腿1", "哈哈",
                "中国1", "美国", "俄罗斯", "德国", "法国1", "意大利", "日本", "韩国", "英国1", "西班牙",
                "吃的", "喝的百事可乐还是可口可乐", "谁的天涯明月刀", "我的中华人民共和国", "天涯", "令牌1", "汉堡, 鸡翅， 牛排， 盒饭",
                "炸鸡", "肥牛", "又一只肥牛， 可以了吗", "鸡腿1", "哈哈2345",
                "鸡腿1", "哈哈",
                "中国1", "美国", "俄罗斯", "德国", "法国1", "意大利", "日本", "韩国", "英国1", "西班牙",
                "吃的", "喝的百事可乐还是可口可乐", "谁的天涯明月刀", "我的中华人民共和国", "天涯", "令牌1", "汉堡, 鸡翅， 牛排， 盒饭",
                "炸鸡", "肥牛", "又一只肥牛， 可以了吗", "鸡腿1", "哈哈2345"));

        recyclerView.setAdapter(adapter);
        findViewById(R.id.btn_action).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 更换美国和日本
                dataList.add(6, "美国");
                dataList.remove(1);
                adapter.notifyItemMoved(1, 5);
            }
        });
        adapter.setOnItemClick(new BaseAdapter.OnItemClickListener<TagsViewHolder>() {
            @Override
            public boolean onItemClick(TagsViewHolder holder) {
                Log.v(TAG, "onItemCLick, and str is " + dataList.get(holder.getAdapterPosition()));
                return false;
            }
        });
    }

    private RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
        }
    };

    private GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            String content = dataList.get(position);
            if (content.endsWith("1")){
                return 4;
            }
            return 1;
        }
    };
}
